package com.wxxymakers.grademark.controller;

import com.wxxymakers.grademark.model.entity.WxUser;
import com.wxxymakers.grademark.utils.Const;
import com.wxxymakers.grademark.utils.RandomVerify;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;

/**
 * @AUTHOR soft
 * @DATE 2018/12/22 19:10
 * @DESCRIBE
 */
@Controller
public class ViewController {

    /**
     * 主页
     */
    @GetMapping({"/", "/home"})
    public String index() {
        return "home";
    }

    /**
     * 个人记录
     */
    @GetMapping("/record")
    public String record() {
        return "record";
    }

    /**
     * 个人信息修改
     */
    @GetMapping("/setting")
    public String setting(@SessionAttribute(Const.CURRENT_USER) WxUser user, ModelMap map) {
        map.put("setu", user);
        return "setting";
    }

    @GetMapping("/safe")
    public String safe() {
        return "safe";
    }

    /**
     * 登录页面
     */
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 注册页面
     */
    @GetMapping("/register")
    public String register() {
        return "register";
    }

    /*
     * 随机验证码
     */
    @RequestMapping("/verify")
    public ResponseEntity<byte[]> verify(HttpServletRequest request) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String code = RandomVerify.create(out);
        request.setAttribute("status", code);
        request.getSession().setAttribute(RandomVerify.S_KEY, code);
        return ResponseEntity.status(200)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=v.png")
                .body(out.toByteArray());
    }

}
